# _*_ coding: UTF-8 _*_
# @Time     : 2021/10/28 下午 01:54
# @Author   : Li Jie
# @Site     : http://www.hzdledu.cn/
# @File     : c14s1_异常.py
# @Software : PyCharm

# 异常
# 异常是难以预知，在程序可能出现的错误，中断程序的运行
def foo1():
    for i in range(-2, 3):
        print(50 / i)


# 异常处理
# 1.try-except
def foo2():
    for i in range(-2, 3):
        try:
            print(50 / i)
        except Exception as e:
            print(e)


# tr-except-else
def foo3():
    for i in range(-2, 3):
        try:
            print(50 / i)
        except Exception as e:
            print(e)
        else:
            print('没有异常')
        print(f'-----第{i}次循环-------')


# tr-except-else-finally
def foo4():
    for i in range(-2, 3):
        try:
            print(50 / i)
        except Exception as e:
            print(e)
        else:
            print('没有异常')
        finally:
            print('不管有没有异常都要执行')
        print(f'-----第{i}次循环-------')


# raise抛出异常
def div(a, b):
    if b == 0:
        raise ZeroDivisionError('除数为0')
    return a / b


def foo5():
    a = int(input('输一个数：'))
    b = int(input('再输一个数：'))
    try:
        print(div(a, b))
    except Exception as e:
        print(e)


if __name__ == "__main__":
    # foo1()
    # foo2()
    # foo3()
    # foo4()
    foo5()